import uviewPlus from 'uview-plus'
import "uview-plus/index.scss"

import { createSSRApp } from "vue"
import App from "./App.vue"
export function createApp() {
	const app = createSSRApp(App)
	app.use(uviewPlus)

	// const rootValue = 16
	// const rootWidth = 390
	// const deviceWidth = document.documentElement.clientWidth
	// document.documentElement.style.fontSize = deviceWidth * rootValue / rootWidth + 'px'

	// let baseUrl = "http://192.168.200.3:8091/shop-wx-api"
	// let baseUrl = "http://localhost:8000/show-wx-api"
	let baseUrl = "http://192.168.63.73:8091/shop-wx-api"
	app.config.globalProperties.$baseUrl = baseUrl //设置全局变量$baseUrl

	let minioUrl = "http://114.132.57.246:9000/shop"
	app.config.globalProperties.$minioUrl = minioUrl

	app.config.globalProperties.api = {
		getGoodsList: baseUrl + "/goods/getGoodsList",
		getAllSort: baseUrl + "/sort/getAllSort",
		getGoodsById: baseUrl + "/goods/getGoodsById",
		joinCart: baseUrl + "/cart/joinCart",
		getCart: baseUrl + "/cart/getCart",
		deleteCartItem: baseUrl + "/cart/deleteCartItem",
		loginOrRegister: baseUrl + "/user/loginOrRegister",
		updatePhoto: baseUrl + "/user/updatePhoto",
		joinAddress: baseUrl + "/userInfo/joinAddress",
		getAddress: baseUrl + "/userInfo/getAddress",
		deleteAddress: baseUrl + "/userInfo/deleteAddress",
		getAddressByAid: baseUrl + "/userInfo/getAddressByAid",
		updateAddress: baseUrl + "/userInfo/updateAddress",
		getCollection: baseUrl + "/userInfo/getCollection",
		joinCollection: baseUrl + "/userInfo/joinCollection",
		cancelCollection: baseUrl + "/userInfo/cancelCollection",
		getCreateOrderInfo: baseUrl + "/order/getCreateOrderInfo",
		createOrder: baseUrl + "/order/createOrder",
		getUserMoney: baseUrl + "/userInfo/getUserMoney",
		getTSort: baseUrl + "/sort/getTSort"
	}

	app.config.globalProperties.ajax = (url: any, method: any, data: any, fun: any, load: any) => {
		let timer = null
		if (load == true || load == undefined) {
		  uni.showLoading({
			title: "努力加载中"
		  })
		  timer = setTimeout(function() {
			uni.hideLoading()
		  }, 60 * 1000)
	}
    uni.request({
		"url": url,
		"method": method,
		"header": {
        token: uni.getStorageSync("token")
    },
		"data": data,
		success: function(resp) {
			if (load == true || load == undefined) {
			clearTimeout(timer)
			uni.hideLoading()
        }
        if (resp.statusCode == 401) {
			uni.removeStorageSync("token")
				uni.showToast({
				icon: "error",
				title: "请登录小程序"
			})
			setTimeout(() => {
				uni.switchTab({
				  "url":"/pages/mine/mine"
				})
			}, 2000);
        } else if (resp.statusCode == 200 && resp.data.code == 200) {
			let data = resp.data
			if (data.hasOwnProperty("token")) {
				let token = data.token
				uni.setStorageSync("token", token)
			}
			fun(resp)
        } else {
			uni.showToast({
				icon: "none",
				title: "执行异常"
			})
			console.error(resp.data)
        }
    },
    fail: function(error) {
        if (load == true || load == undefined) {
          clearTimeout(timer)
          uni.hideLoading()
        }
      }
    })
  }

  return {
    app,
  };
}
